package larrys.pomodoro.util;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;

final public class AjaxResponseUtil {

	public final static Gson GSON = new Gson();
	private final static String SUCCESS_RESPONSE = buildMapJson("success",
			"true");

	/**
	 * eg: input: [key1, value2, key2, value2] output:
	 * {"key1":"value2","key2","value2"}
	 * 
	 * @param keyAndValue
	 *            a variable argument with format: [key, value, key value]
	 * @return json
	 */
	public static String buildMapJson(String... keyAndValue) {
		Map<String, String> resultMap = new HashMap<String, String>();
		for (int i = 0; i < keyAndValue.length - 1; i++) {
			String key = keyAndValue[i];
			String value = i++ < keyAndValue.length ? keyAndValue[i] : "";
			resultMap.put(key, value);
		}
		return GSON.toJson(resultMap);
	}

	public static String buildFailureWithMsgResponse(String message) {
		return buildMapJson("success", "false", "message", message);
	}

	public static String buildSuccessWithMsgResponse(String message) {
		return buildMapJson("success", "true", "message", message);
	}

	public static String buildSuccessResponse() {
		return SUCCESS_RESPONSE;
	}
}
